1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.primitives;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.testing.Helpers;
21  
22  import junit.framework.TestCase;
23  
24  import java.util.Arrays;
25  import java.util.Comparator;
26  import java.util.List;
27  
28  /**
29   * Unit test for {@link SignedBytes}.
30   *
31   * @author Kevin Bourrillion
32   */
33  @GwtCompatible(emulated = true)
34  @SuppressWarnings("cast") // redundant casts are intentional and harmless
35  public class SignedBytesTest extends TestCase {
36    private static final byte[] EMPTY = {};
37    private static final byte[] ARRAY1 = {(byte) 1};
38  
39    private static final byte LEAST = Byte.MIN_VALUE;
40    private static final byte GREATEST = Byte.MAX_VALUE;
41  
42    private static final byte[] VALUES =
43        {LEAST, -1, 0, 1, GREATEST};
44  
45    public void testCheckedCast() {
46      for (byte value : VALUES) {
47        assertEquals(value, SignedBytes.checkedCast((long) value));
48      }
49      assertCastFails(GREATEST + 1L);
50      assertCastFails(LEAST - 1L);
51      assertCastFails(Long.MAX_VALUE);
52      assertCastFails(Long.MIN_VALUE);
53    }
54  
55    public void testSaturatedCast() {
56      for (byte value : VALUES) {
57        assertEquals(value, SignedBytes.saturatedCast((long) value));
58      }
59      assertEquals(GREATEST, SignedBytes.saturatedCast(GREATEST + 1L));
60      assertEquals(LEAST, SignedBytes.saturatedCast(LEAST - 1L));
61      assertEquals(GREATEST, SignedBytes.saturatedCast(Long.MAX_VALUE));
62      assertEquals(LEAST, SignedBytes.saturatedCast(Long.MIN_VALUE));
63    }
64  
65    private static void assertCastFails(long value) {
66      try {
67        SignedBytes.checkedCast(value);
68        fail("Cast to byte should have failed: " + value);
69      } catch (IllegalArgumentException ex) {
70        assertTrue(value + " not found in exception text: " + ex.getMessage(),
71            ex.getMessage().contains(String.valueOf(value)));
72      }
73    }
74  
75    public void testCompare() {
76      for (byte x : VALUES) {
77        for (byte y : VALUES) {
78          // Only compare the sign of the result of compareTo().
79          int expected = Byte.valueOf(x).compareTo(y);
80          int actual = SignedBytes.compare(x, y);
81          if (expected == 0) {
82            assertEquals(x + ", " + y, expected, actual);
83          } else if (expected < 0) {
84            assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
85                actual < 0);
86          } else {
87            assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
88                actual > 0);
89          }
90        }
91      }
92    }
93  
94    public void testMax_noArgs() {
95      try {
96        SignedBytes.max();
97        fail();
98      } catch (IllegalArgumentException expected) {
99      }
100   }
101 
102   public void testMax() {
103     assertEquals(LEAST, SignedBytes.max(LEAST));
104     assertEquals(GREATEST, SignedBytes.max(GREATEST));
105     assertEquals((byte) 127, SignedBytes.max(
106         (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1));
107   }
108 
109   public void testMin_noArgs() {
110     try {
111       SignedBytes.min();
112       fail();
113     } catch (IllegalArgumentException expected) {
114     }
115   }
116 
117   public void testMin() {
118     assertEquals(LEAST, SignedBytes.min(LEAST));
119     assertEquals(GREATEST, SignedBytes.min(GREATEST));
120     assertEquals((byte) -128, SignedBytes.min(
121         (byte) 0, (byte) -128, (byte) -1, (byte) 127, (byte) 1));
122   }
123 
124   public void testJoin() {
125     assertEquals("", SignedBytes.join(",", EMPTY));
126     assertEquals("1", SignedBytes.join(",", ARRAY1));
127     assertEquals("1,2", SignedBytes.join(",", (byte) 1, (byte) 2));
128     assertEquals("123", SignedBytes.join("", (byte) 1, (byte) 2, (byte) 3));
129     assertEquals("-128,-1", SignedBytes.join(",", (byte) -128, (byte) -1));
130   }
131 
132   public void testLexicographicalComparator() {
133     List<byte[]> ordered = Arrays.asList(
134         new byte[] {},
135         new byte[] {LEAST},
136         new byte[] {LEAST, LEAST},
137         new byte[] {LEAST, (byte) 1},
138         new byte[] {(byte) 1},
139         new byte[] {(byte) 1, LEAST},
140         new byte[] {GREATEST, GREATEST - (byte) 1},
141         new byte[] {GREATEST, GREATEST},
142         new byte[] {GREATEST, GREATEST, GREATEST});
143 
144     Comparator<byte[]> comparator = SignedBytes.lexicographicalComparator();
145     Helpers.testComparator(comparator, ordered);
146   }
147 }
148